package com.vf.cloud.scene.mvc.biz.controller;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.common.datasource.annotation.Tx;
import com.vf.cloud.common.util.Pinyin4jUtil;
import com.vf.cloud.common.util.R;
import com.vf.cloud.common.util.UuidUtil;
import com.vf.cloud.scene.common.domain.SubLayer;
import com.vf.cloud.scene.common.domain.SubMenu;
import com.vf.cloud.scene.common.domain.SubMenuLayer;

@RestController
@RequestMapping("/scene/biz/menu")
public class SceneMenuController {

	@GetMapping("/findList")
	public R<List<SubMenu>> findList(@RequestParam(name = "rootId") String rootId,
			@RequestParam(name = "projectId") String projectId) {
		List<SubMenu> menus = SubMenu.dao.getTableTree(rootId, projectId);
		return R.ok(menus);
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SubMenu> save(SubMenu subMenu) {
		try {
			if (StrKit.isBlank(subMenu.getName())) {
				return R.failed("名称不可以为空！");
			}
			if (StrKit.notBlank(subMenu.getId())) {
				if (subMenu.update()) {
					return R.ok(subMenu);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				subMenu.setId(UuidUtil.getUUID());
				if (StrKit.isBlank(subMenu.getParentId()))
					subMenu.setParentId("0");
				if (StrKit.isBlank(subMenu.getCode())) {
					subMenu.setCode(Pinyin4jUtil.getPinYin(subMenu.getName()));
				}
				if (subMenu.save()) {
					return R.ok(subMenu);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public R<String> delete(@RequestParam(name = "id") String id) throws Exception {
		try {
			SubMenu sceneSrc = SubMenu.dao.findById(id);
			if (sceneSrc != null) {
				if (sceneSrc.delete()) {
					return R.ok();
				} else {
					return R.failed("删除失败！");
				}
			} else {
				return R.failed("数据不存在,请刷新后再试!");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/deleteMenuLayer", method = RequestMethod.GET)
	public R<String> deleteMenuLayer(@RequestParam(name = "menuId") String menuId,
			@RequestParam(name = "layerId") String layerId) throws Exception {
		try {
			SubMenuLayer subMenuLayer = SubMenuLayer.dao.findFirst(
					"SELECT * FROM " + SubMenuLayer.TABLE_NAME + " WHERE MENU_ID=? AND LAYER_ID=?", menuId, layerId);
			if (subMenuLayer != null) {
				if (subMenuLayer.delete()) {
					return R.ok();
				} else {
					return R.failed("删除失败！");
				}
			} else {
				return R.failed("数据不存在,请刷新后再试!");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/getOptionsTree", method = RequestMethod.GET)
	public R<List<SubMenu>> getOptionsTree(
			@RequestParam(name = "parentId", required = false, defaultValue = "0") String parentId,
			@RequestParam(name = "type", required = false) String type) {
		return R.ok(SubMenu.dao.getTree(parentId, type, ""));
	}

	@GetMapping("/getTree")
	public R<List<SubMenu>> getTree(@RequestParam(name = "rootId") String rootId,
			@RequestParam(name = "sceneId", required = false) String sceneId) {
		List<SubMenu> menus = SubMenu.dao.getTree(rootId, "", sceneId);
		return R.ok(menus);
	}

	@RequestMapping(value = "/getLayerByMenu", method = RequestMethod.GET)
	public R<List<SubMenuLayer>> getLayerByMenu(@RequestParam(name = "projectId") String projectId,
			@RequestParam(name = "menuId") String menuId) throws Exception {
		try {
			SubMenu sceneMenu = SubMenu.dao.findById(menuId);
			if (sceneMenu == null) {
				return R.failed("数据不存在,请刷新后再试!");
			}
			List<SubMenuLayer> subMenuLayers = SubMenuLayer.dao.find(
					"SELECT L.*,(SELECT NAME FROM " + SubLayer.TABLE_NAME + " WHERE ID=L.LAYER_ID ) AS layerName FROM "
							+ SubMenuLayer.TABLE_NAME + " L WHERE L.PROJECT_ID=? AND L.MENU_ID=? ORDER BY L.SORT ",
					projectId, sceneMenu.getId());
			return R.ok(subMenuLayers);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/saveMenuLayers", method = RequestMethod.POST)
	public R<String> saveMenuLayers(@RequestParam String projectId, @RequestParam String menuId,
			@RequestParam String layerIds) {
		return SubMenu.saveMenuLayers(projectId, menuId, layerIds);
	}

	@Tx
	@RequestMapping(value = "/updateMenuLayer", method = RequestMethod.POST)
	public R<SubMenuLayer> updateMenuLayer(SubMenuLayer subMenuLayer) {
		try {
			if (StrKit.isBlank(subMenuLayer.getMenuId()) || StrKit.isBlank(subMenuLayer.getLayerId())) {
				return R.failed("参数必填！");
			}
			if (subMenuLayer.update()) {
				return R.ok(subMenuLayer);
			} else {
				return R.failed("更新失败！");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}
	
}
